package com.djx.dao.impl;

import com.djx.dao.BookDao;
import com.djx.domian.Book;
import com.djx.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {
    private Connection con;
    private Statement stmt;
    private PreparedStatement pstmt;

    public int save(Book book) {
        con = JDBCUtils.getConnection();
        int result = 0;
        try {
            String sql = "insert into book values(null,?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, book.getType());
            pstmt.setString(2, book.getName());
            pstmt.setString(3, book.getDescription());
            result = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return result;
    }

    public int update(Book book) {
        con = JDBCUtils.getConnection();
        int result = 0;
        try {
            String sql = "update book set type = ?, name = ?, description = ? where id = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(4, book.getId());
            pstmt.setString(1, book.getType());
            pstmt.setString(2, book.getName());
            pstmt.setString(3, book.getDescription());
            result = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return result;
    }

    public int delete(Integer id) {
        con = JDBCUtils.getConnection();
        int result = 0;
        try {
            String sql = "delete from book where id = " + id;
            stmt = con.createStatement();
            result = stmt.executeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return result;
    }


    public List<Book> getAll() {
        con = JDBCUtils.getConnection();
        ArrayList<Book> books = new ArrayList<>();
        ResultSet rs = null;
        try {
            stmt = con.createStatement();
            String sql = "select * from book";

            rs = stmt.executeQuery(sql);
            if (!rs.isBeforeFirst()) {
                return null;
            }
            while (rs.next()) {
                Integer id = rs.getInt("id");
                String type = rs.getString("type");
                String name = rs.getString("name");
                String description = rs.getString("description");
                Book book = new Book(id, type, name, description);
                books.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }


        }
        return books;
    }
}
